//
//  LocationTrigger.m
//  aloqa
//
//  Created by mkoo sun on 12-8-26.
//  Copyright (c) 2012年 mogenesis. All rights reserved.
//
#import "LocationTrigger.h"
#import "JSONKit.h"
#import "ServiceManager.h"

@implementation LocationTrigger

//@synthesize locationManager;
@synthesize lastLocation;
@synthesize address;
@synthesize city;

//-------------------------------------------------------------------------------------------
- (id)init
{
    self = [super init];
    if (self) {
//        locationManager = [[CLLocationManager alloc] init];
//        [locationManager setDelegate:self];
//        [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];        
//        [locationManager startUpdatingLocation];
        
        address = @"";
        city = @"";
    }
    return self;
}

- (void)dealloc
{
    [locationManager release];
    locationManager = nil;
    [super dealloc];
}

//-------------------------------------------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
    lastLocation = newLocation.coordinate;
    
    //上报位置
    long long time = [[NSDate date]timeIntervalSince1970];
    if(time - lastLocationTime >= 5*60*1000)
    {
        lastLocationTime = time;
        
        ReminderService* reminderService = [[ServiceManager getInstance]getService:REMINDER_SERVICE];
        if([reminderService.reminderList count]>0)
        {
            /*
            Reminder* reminder = [reminderService.reminderList objectAtIndex:0];
            
            NSMutableDictionary* geo_sax = [[NSMutableDictionary alloc]init];
            
            [geo_sax setValue: [NSNumber numberWithDouble: lastLocation.latitude] 
                       forKey:@"lat"];
            [geo_sax setValue: [NSNumber numberWithDouble: lastLocation.longitude] 
                       forKey:@"lon"];
            
            NSString* jsonStr = [geo_sax JSONString];
            
            [reminderService NotifyReminder:reminder type:@"geo" data:jsonStr];
            
            [geo_sax release];
            */
        }
    }
}

//-------------------------------------------------------------------------------------------
- (void) notifyLocationEvent
{
    //NSUInteger
    NSMutableDictionary* sax = [[NSMutableDictionary alloc]init];

    [sax setValue: [NSNumber numberWithDouble: lastLocation.longitude] 
           forKey:@"longitude"];
    
    [sax setValue:[NSNumber numberWithDouble: lastLocation.latitude] 
           forKey:@"latitude"];
    
    [sax setValue:address forKey:@"address"];
    [sax setValue:city forKey:@"city"];
    
    /*
    sax.writeString("longitude");
    sax.writeDouble(lastLocation.getLongitude());
    sax.writeString("latitude");
    sax.writeDouble(lastLocation.getLatitude());
    sax.writeString("accuracy");
    sax.writeDouble(lastLocation.getAltitude());
    sax.writeString("bearing");
    sax.writeDouble(lastLocation.getSpeed());
    sax.writeString("time");
    sax.writeString(lastLocation.getTime());
    sax.writeString("address");
    sax.writeString(address);
    sax.writeString("city");
    sax.writeString(city);
    sax.endMap();
    */
//    try {
//        service.signEvent(TransactionConstant.event_location,
//                          writer.toString(), "");
//    } catch (Exception e) {
//    }
    
    
    
    NSString* json = [sax JSONString];
    //NSLog(json, nil);
    
    //NSLog(json,nil);
    EventService* eventService = [[ServiceManager getInstance]getService:EVENT_SERVICE];    
    [eventService signEvent:TransactionConstant_event_location param1:json param2:@""];
    
    [sax release];
}

//-------------------------------------------------------------------------------------------
-(void) onEvent:(NSString*)aId param1:(NSString *)param1 param2:(NSString *)param2
{
    if([aId isEqualToString:TransactionConstant_event_req_location])
    {
        address = @"贡院西街";
        city = @"北京";
        [self notifyLocationEvent];
    }
}

//-------------------------------------------------------------------------------------------
- (void) start
{
    
}

- (void) stop
{
}

@end
